package cn.iocoder.yudao.module.contract.dal.mysql.frameworkAgreement;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.contract.dal.dataobject.frameworkAgreement.FrameworkAgreementDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.orderContract.OrderContractDO;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.contract.controller.admin.frameworkAgreement.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 框架协议 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface FrameworkAgreementMapper extends BaseMapperX<FrameworkAgreementDO> {

//    default PageResult<FrameworkAgreementDO> selectPage(FrameworkAgreementPageReqVO reqVO) {
//        return selectPage(reqVO, new LambdaQueryWrapperX<FrameworkAgreementDO>()
//                .likeIfPresent(FrameworkAgreementDO::getContractTemplateName, reqVO.getContractTemplateName())
//                .likeIfPresent(FrameworkAgreementDO::getContractNo, reqVO.getContractNo())
//                .likeIfPresent(FrameworkAgreementDO::getClientName, reqVO.getClientName())
//                .likeIfPresent(FrameworkAgreementDO::getCompanyNames, reqVO.getCompanyNames())
//                .betweenIfPresent(FrameworkAgreementDO::getCreateTime, reqVO.getCreateTime())
//                .orderByDesc(FrameworkAgreementDO::getId));
//    }

    default PageResult<FrameworkAgreementRespVO> selectPage(FrameworkAgreementPageReqVO reqVO) {
        return selectJoinPage(reqVO,FrameworkAgreementRespVO.class, new MPJLambdaWrapperX<FrameworkAgreementDO>()
                .selectAll(FrameworkAgreementDO.class)
                .selectAs(OrderContractDO::getId, FrameworkAgreementRespVO::getOrderContractId)
                .betweenIfPresent(FrameworkAgreementDO::getCreateTime, reqVO.getCreateTime())
                .betweenIfPresent(FrameworkAgreementDO::getAgreementStartDate, reqVO.getAgreementStartDate())
                .like(!ObjectUtils.isEmpty(reqVO.getContractTemplateName()),FrameworkAgreementDO::getContractTemplateName, reqVO.getContractTemplateName())
                .like(!ObjectUtils.isEmpty(reqVO.getContractNo()),FrameworkAgreementDO::getContractNo, reqVO.getContractNo())
                .like(!ObjectUtils.isEmpty(reqVO.getClientName()),FrameworkAgreementDO::getClientName, reqVO.getClientName())
                .like(!ObjectUtils.isEmpty(reqVO.getClientCode()),FrameworkAgreementDO::getClientCode, reqVO.getClientCode())
                .like(!ObjectUtils.isEmpty(reqVO.getCompanyNames()),FrameworkAgreementDO::getCompanyNames, reqVO.getCompanyNames())
                .in(!ObjectUtils.isEmpty(reqVO.getVkinfo()), FrameworkAgreementDO::getSalesDept,  !ObjectUtils.isEmpty(reqVO.getVkinfo()) ?reqVO.getVkinfo().split(","): new Object[]{""})

//                .leftJoin(OrderContractDO.class, OrderContractDO::getFrameworkAgreementId, FrameworkAgreementDO::getId)

                .leftJoin(OrderContractDO.class, on -> on
                        .eq(OrderContractDO::getFrameworkAgreementId,FrameworkAgreementDO::getId)
                        .inSql(OrderContractDO::getId,"select MAX(b.id) from ct_order_contract b where b.framework_agreement_id=t.id"))

                .isNotNull("1".equals(reqVO.getIsValid()),OrderContractDO::getId)
                .isNull("0".equals(reqVO.getIsValid()),OrderContractDO::getId)
                .orderByDesc(FrameworkAgreementDO::getId));
    }

//    @Select("SELECT * FROM `ct_framework_agreement` WHERE and clientName =#{clientName}" +
//            "and (( agreement_start_date >= #{agreementStartDate} AND agreement_start_date <= #{agreementEndDate} ) " +
//            "OR ( agreement_start_date <= #{agreementStartDate} AND agreement_end_date >= #{agreementEndDate} ) " +
//            "OR ( agreement_end_date >= #{agreementStartDate} AND agreement_end_date <= #{agreementEndDate} ))")
    int countClientDate(@Param("id") Long id, @Param("clientName") String clientName, @Param("agreementStartDate") String agreementStartDate, @Param("agreementEndDate") String agreementEndDate);
}